package com.nineclock.message.service.impl;

import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NcException;
import com.nineclock.common.utils.BeanHelper;
import com.nineclock.message.dto.NcMessageDto;
import com.nineclock.message.dto.NcMessageQueryDTO;
import com.nineclock.message.jpush.JPushManager;
import com.nineclock.message.pojo.NcMessage;
import com.nineclock.message.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.mongodb.core.query.UpdateDefinition;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;


@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private JPushManager jPushManager;

    @Override
    public void pushMessage(NcMessage ncMessage) throws Exception {

        // 1.将消息保存到mongoDB
        ncMessage.setCreateTime(new Date());
        mongoTemplate.insert(ncMessage);


        // 2.调用极光api推送通知
        jPushManager.sendNotificationByAlias(ncMessage.getTitle(), ncMessage.getContent(), ncMessage.getTargets(), null);

    }

    @Override
    public List<NcMessageDto> findMessageRecord(NcMessageQueryDTO messageQueryDTO) {
        // 参数校验
        if(messageQueryDTO == null){
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }

        // 创建查询对象
        Query query = new Query();

        if(!StringUtils.isEmpty(messageQueryDTO.getId())){
            query.addCriteria(new Criteria("_id").is(messageQueryDTO.getId()));
        }

        if(!StringUtils.isEmpty(messageQueryDTO.getType())){ // 消息类型
            query.addCriteria(new Criteria("messageType").is(messageQueryDTO.getType()));
        }

        if(!StringUtils.isEmpty(messageQueryDTO.getUserId())){ // 申请用户id
            query.addCriteria(new Criteria("applyUserId").is(messageQueryDTO.getUserId()));
        }

        if(!StringUtils.isEmpty(messageQueryDTO.getTargetId())){ // 审批用户id
            query.addCriteria(new Criteria("approveUserId").is(messageQueryDTO.getTargetId()));
        }

        // 设置排序参数，根据审核状态升序排序
        query.with(Sort.by(Sort.Order.asc("approveStatue")));

        List<NcMessage> ncMessageList = mongoTemplate.find(query, NcMessage.class);


        if(!CollectionUtils.isEmpty(ncMessageList)){
            List<NcMessageDto> ncMessageDtoList = BeanHelper.copyWithCollection(ncMessageList, NcMessageDto.class);
            return ncMessageDtoList;
        }


        return null;
    }

    @Override
    public void updateMessageWithApproved(String msgId) {
        Query query = new Query(new Criteria("_id").is(msgId));

        Update update = new Update();
        update.set("useStatus", 1); // 已读
        update.set("approveStatue", 1);

        mongoTemplate.updateMulti(query, update, NcMessage.class);
    }
}
